package uk.co.droidinactu.sony.prst1.home.data;

/// <summary>
/// Description of AbstractDataObj.

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.provider.BaseColumns;

/**
 * this is the base class of all objects that are stored in a database.
 * 
 * @author andy
 */
public abstract class AbstractDataObj {

    /**
     * The name of the column in the database used to store the created date/time.
     */
    public static String COLUMN_NAME_ADDED_DATE = "added_date";

    /**
     * The name of the column in the database used to store the modified date/time.
     */
    public static String COLUMN_NAME_MODIFIED_DATE = "modified_date";

    /**
     * this is the unique id of the object in the database.
     */
    public long _id = -1;

    public long added_date;
    public long modified_date;

    /**
     * Simple default constructor for data objects
     */
    public AbstractDataObj() {
    }

    public AbstractDataObj(final Cursor tmpCursor) {
        try {
            _id = tmpCursor.getInt(tmpCursor.getColumnIndex(BaseColumns._ID));
        } catch (final Exception e) {
        }
        try {
            added_date = tmpCursor.getLong(tmpCursor.getColumnIndex(COLUMN_NAME_ADDED_DATE));
        } catch (final Exception e) {
        }
        try {
            modified_date = tmpCursor.getLong(tmpCursor.getColumnIndex(COLUMN_NAME_MODIFIED_DATE));
        } catch (final Exception e) {
        }
    }

    public final ArrayList<String> getArrayList(final String val1, final String val2) {
        final ArrayList<String> ary = new ArrayList<String>();
        ary.add(val1);
        ary.add(val2);
        return ary;
    }

    public ContentValues getContentValues() {
        final ContentValues map = new ContentValues();
        if (_id > -1) {
            map.put(BaseColumns._ID, _id);
        }
        map.put(COLUMN_NAME_ADDED_DATE, added_date);
        map.put(COLUMN_NAME_MODIFIED_DATE, modified_date);
        return map;
    }

    public String getSelectAllSql() {
        return ("select * from " + getTableName() + "");
    }

    public String getSelectByIdSql(final Long objId) {
        return ("select * from " + getTableName() + " where " + BaseColumns._ID + "=" + objId);
    }

    public String getSqlDeleteAll() {
        return ("delete from " + getTableName());
    }

    public abstract String getTableName();

    public String makeSafeSQL(final String str) {
        String tmpStr = str.replace('"', '`');
        tmpStr = tmpStr.replace('\'', '`');
        tmpStr = tmpStr.replace('\\', '_');

        return tmpStr;
    }

    protected String removeQuotes(final String string) {
        String rtStr = string;
        if (rtStr.startsWith("\"")) {
            rtStr = rtStr.substring(1);
        }
        if (rtStr.endsWith("\"")) {
            rtStr = rtStr.substring(0, rtStr.length() - 1);
        }
        return rtStr;
    }

} // class()

